﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class DanhSachHangHoa : Office2007Form
    {

        public event EventHandler Button_Clicked;

        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        public DanhSachHangHoa()
        {
            InitializeComponent();
        }
        void HangHoa()
        {
            var hanghoa = from hh in BO.DanhSachHangHoaBO.DanhSachHangHoa()
                           select new { hh.MaHangHoa, hh.TenHangHoa, hh.NHACUNGCAP.TenNhaCungCap, hh.LOAIHANGHOA.TenLoaiHangHoa,hh.XuatXu,hh.DONVITINH.TenDonViTinh };
            gvHangHoa.DataSource = hanghoa;
        }
        private void DanhSachHangHoa_Load(object sender, EventArgs e)
        {
            cbLoaiHangHoa.DataSource = BO.DanhSachHangHoaBO.DanhSachLoaiHangHoa();
            cbLoaiHangHoa.DisplayMember = "TenLoaiHangHoa";
            cbLoaiHangHoa.ValueMember = "MaLoaiHangHoa";

            cbNhaCungCap.DataSource = BO.DanhSachHangHoaBO.DanhSachNhaCungCap();
            cbNhaCungCap.DisplayMember = "TenNhaCungCap";
            cbNhaCungCap.ValueMember = "MaNhaCungCap";

            cbDonViTinh.DataSource = BO.DanhSachHangHoaBO.DanhSachDonViTinh();
            cbDonViTinh.DisplayMember = "TenDonViTinh";
            cbDonViTinh.ValueMember = "MaDonViTinh";
            HangHoa();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            if (this.Button_Clicked != null)
            {
                this.Button_Clicked(sender, e);
            }
            this.Close();
        }

        private void gvHangHoa_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;

                ThemBaoGiaForm.machitiet = gvHangHoa.Rows[index].Cells["MaHangHoa"].Value.ToString().Trim();
                QuanLyPhieuBaoGia.machitiet = gvHangHoa.Rows[index].Cells["MaHangHoa"].Value.ToString().Trim();
            }
            catch
            {
                MessageBox.Show("Không có hàng hóa nào");
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
           
        }

        private void btnThem_Click_1(object sender, EventArgs e)
        {
            if (txtTenHangHoa.Text.Trim() == "")
            {
                MessageBox.Show("Tên hàng hóa không được để trống");
                txtTenHangHoa.Focus();
            }
            else
            {
                string mahanghoa;
                if (BO.DanhSachHangHoaBO.HangHoaCuoiCung() != null)
                    mahanghoa = NextID(BO.DanhSachHangHoaBO.HangHoaCuoiCung().MaHangHoa, "HH");
                else
                    mahanghoa = "HH0001";
                BO.DanhSachHangHoaBO.ThemHangHoa(mahanghoa, txtTenHangHoa.Text, cbLoaiHangHoa.SelectedValue.ToString(), cbNhaCungCap.SelectedValue.ToString(), txtXuatXu.Text.Trim(), null, cbDonViTinh.SelectedValue.ToString().Trim(), null, null, null);

                HangHoa();

                txtTenHangHoa.Text = "";
                txtTenHangHoa.Focus();
                txtXuatXu.Text = "";
                MessageBox.Show("Bạn đã thêm thành công");
            }
        }

        private void btnChon_Click_1(object sender, EventArgs e)
        {
            if (this.Button_Clicked != null)
            {
                this.Button_Clicked(sender, e);
            }
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
